package config

import (
	"fmt"
	"github.com/spf13/viper"
)

type mysql struct {
	Host     string `validate:"required"`
	Port     string `validate:"required"`
	Name     string `validate:"required"`
	Username string `validate:"required"`
	Password string `validate:"required"`
	Params   string `validate:"required"`
	LogMode  int    `validate:"required"`
	MaxIdle  int    `validate:"required"`
	MaxOpen  int    `validate:"required"`
}
type logger struct {
	Filepath string
}
type crawler struct {
	DownloadPicPath string `validate:"required"`
	PicPath         string `validate:"required"`
}

var (
	MysqlConfig   mysql
	LoggerConfig  logger
	CrawlerConfig crawler
)

var Profile = "local"

func initEnv(env string) {
	viper.SetConfigName(fmt.Sprintf("application-%s", env))
	err := viper.ReadInConfig()
	if err != nil {
		panic(err)
	}
	setEnvConfigDefaultValue()
	c := viper.AllSettings()
	read(c["mysql"], &MysqlConfig)
	read(c["logger"], &LoggerConfig)
	read(c["crawler"], &CrawlerConfig)

	validateEnv()
}

func setEnvConfigDefaultValue() {
	MysqlConfig.setDefault()
}

func validateEnv() {
	MysqlConfig.validate()
}

func (s *mysql) validate() {
	err := validate.Struct(s)
	if err != nil {
		panic(err)
	}
}

func (s *mysql) setDefault() {

}
